package cn.iocoder.yudao.module.system.controller.admin.teacherdashboard;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.teacherdashboard.vo.*;
import cn.iocoder.yudao.module.system.service.teacherdashboard.TeacherDashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 教师看板 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 教师看板")
@RestController
@RequestMapping("/system/teacher-dashboard")
@Validated
@Slf4j
public class TeacherDashboardController {

    @Resource
    private TeacherDashboardService teacherDashboardService;

    @GetMapping("/get")
    @Operation(summary = "获取教师看板数据")
    public CommonResult<TeacherDashboardRespVO> getTeacherDashboard(@Valid TeacherDashboardReqVO reqVO) {
        TeacherDashboardRespVO dashboard = teacherDashboardService.getTeacherDashboard(reqVO);
        return success(dashboard);
    }

    @GetMapping("/class-overview")
    @Operation(summary = "获取班级概览信息")
    public CommonResult<TeacherDashboardRespVO.ClassBasicInfo> getClassOverview(@Valid ClassOverviewReqVO reqVO) {
        TeacherDashboardRespVO.ClassBasicInfo overview = teacherDashboardService.getClassOverview(reqVO);
        return success(overview);
    }

    @GetMapping("/admission-analysis")
    @Operation(summary = "获取升学分析数据")
    public CommonResult<AdmissionAnalysisRespVO> getAdmissionAnalysis(@Valid AdmissionAnalysisReqVO reqVO) {
        AdmissionAnalysisRespVO analysis = teacherDashboardService.getAdmissionAnalysis(reqVO);
        return success(analysis);
    }

    @GetMapping("/score-statistics")
    @Operation(summary = "获取班级成绩统计")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<TeacherDashboardRespVO.ScoreStatistics> getScoreStatistics(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId) {
        TeacherDashboardRespVO.ScoreStatistics statistics = teacherDashboardService.getClassScoreStatistics(classId, examId);
        return success(statistics);
    }

    @GetMapping("/admission-prediction")
    @Operation(summary = "获取升学预测统计")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<TeacherDashboardRespVO.AdmissionPredictionStats> getAdmissionPrediction(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId) {
        TeacherDashboardRespVO.AdmissionPredictionStats prediction = teacherDashboardService.getAdmissionPredictionStats(classId, examId);
        return success(prediction);
    }

    @GetMapping("/top-students")
    @Operation(summary = "获取班级前N名学生")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @Parameter(name = "topN", description = "前N名", required = false)
    public CommonResult<List<TeacherDashboardRespVO.TopStudentInfo>> getTopStudents(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId,
            @RequestParam(value = "topN", defaultValue = "10") Integer topN) {
        List<TeacherDashboardRespVO.TopStudentInfo> topStudents = teacherDashboardService.getTopStudents(classId, examId, topN);
        return success(topStudents);
    }

    @GetMapping("/improved-students")
    @Operation(summary = "获取进步最大的学生")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "currentExamId", description = "当前考试ID", required = true)
    @Parameter(name = "previousExamId", description = "上次考试ID", required = true)
    @Parameter(name = "limit", description = "限制数量", required = false)
    public CommonResult<List<TeacherDashboardRespVO.ProgressStudentInfo>> getImprovedStudents(
            @RequestParam("classId") Long classId,
            @RequestParam("currentExamId") Long currentExamId,
            @RequestParam("previousExamId") Long previousExamId,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<TeacherDashboardRespVO.ProgressStudentInfo> improvedStudents = 
            teacherDashboardService.getMostImprovedStudents(classId, currentExamId, previousExamId, limit);
        return success(improvedStudents);
    }

    @GetMapping("/attention-students")
    @Operation(summary = "获取需要关注的学生")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "currentExamId", description = "当前考试ID", required = true)
    @Parameter(name = "previousExamId", description = "上次考试ID", required = true)
    public CommonResult<List<TeacherDashboardRespVO.AttentionStudentInfo>> getAttentionStudents(
            @RequestParam("classId") Long classId,
            @RequestParam("currentExamId") Long currentExamId,
            @RequestParam("previousExamId") Long previousExamId) {
        List<TeacherDashboardRespVO.AttentionStudentInfo> attentionStudents = 
            teacherDashboardService.getAttentionStudents(classId, currentExamId, previousExamId);
        return success(attentionStudents);
    }

    @GetMapping("/critical-students")
    @Operation(summary = "获取临界学生分析")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @Parameter(name = "range", description = "临界分数范围", required = false)
    public CommonResult<TeacherDashboardRespVO.CriticalStudentAnalysis> getCriticalStudents(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId,
            @RequestParam(value = "range", defaultValue = "10") Integer range) {
        TeacherDashboardRespVO.CriticalStudentAnalysis criticalStudents = 
            teacherDashboardService.getCriticalStudentAnalysis(classId, examId, range);
        return success(criticalStudents);
    }

    @GetMapping("/class-rank")
    @Operation(summary = "获取班级在年级中的排名")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<Integer> getClassRank(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId) {
        Integer rank = teacherDashboardService.getClassRankInGrade(classId, examId);
        return success(rank);
    }

    @GetMapping("/admission-trend")
    @Operation(summary = "获取升学趋势分析")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "limit", description = "历史考试数量限制", required = false)
    public CommonResult<TeacherDashboardRespVO.AdmissionTrendAnalysis> getAdmissionTrend(
            @RequestParam("classId") Long classId,
            @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        TeacherDashboardRespVO.AdmissionTrendAnalysis trend = 
            teacherDashboardService.getAdmissionTrendAnalysis(classId, limit);
        return success(trend);
    }

    @GetMapping("/comparison")
    @Operation(summary = "获取班级对比分析")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<TeacherDashboardRespVO.ComparisonAnalysis> getComparison(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId) {
        TeacherDashboardRespVO.ComparisonAnalysis comparison = 
            teacherDashboardService.getComparisonAnalysis(classId, examId);
        return success(comparison);
    }

    @GetMapping("/teaching-effect")
    @Operation(summary = "获取教学效果分析")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "currentExamId", description = "当前考试ID", required = true)
    @Parameter(name = "previousExamId", description = "上次考试ID", required = true)
    public CommonResult<TeacherDashboardRespVO.TeachingEffectAnalysis> getTeachingEffect(
            @RequestParam("classId") Long classId,
            @RequestParam("currentExamId") Long currentExamId,
            @RequestParam("previousExamId") Long previousExamId) {
        TeacherDashboardRespVO.TeachingEffectAnalysis effect = 
            teacherDashboardService.getTeachingEffectAnalysis(classId, currentExamId, previousExamId);
        return success(effect);
    }

    @GetMapping("/student-admission-details")
    @Operation(summary = "获取学生详细升学信息")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<List<AdmissionAnalysisRespVO.StudentAdmissionDetail>> getStudentAdmissionDetails(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId) {
        List<AdmissionAnalysisRespVO.StudentAdmissionDetail> details = 
            teacherDashboardService.getStudentAdmissionDetails(classId, examId);
        return success(details);
    }

    // 注意：getAlerts接口已被移除，因为AlertInfo类在优化过程中被删除
    // 相关功能已整合到教学建议中，可通过主接口的teachingSuggestions字段获取

    @GetMapping("/export-report")
    @Operation(summary = "导出班级成绩报告")
    @Parameter(name = "classId", description = "班级ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<String> exportClassReport(
            @RequestParam("classId") Long classId,
            @RequestParam("examId") Long examId) {
        // 这里可以实现报告导出功能
        // 暂时返回成功消息
        return success("报告导出功能开发中");
    }

}
